package com.mooc.ppjoke.ui.splash

import android.util.Log
import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mooc.lib_common.CommonApp
import com.mooc.lib_common.action.BusAction
import com.mooc.lib_common.bus.LiveDataBus
import com.mooc.lib_common.const.BusConst
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashViewModel : ViewModel() {

    var timeStr: ObservableField<String> = ObservableField()
    var startData : LiveDataBus<BusAction> = LiveDataBus()

    fun initData(){
        Log.e("","initData")
        viewModelScope.launch(Dispatchers.IO) {
            //查询用户本地数据
            var list=CommonApp.db.userDao().queryUser()
            if (list!= null && list.size > 0){
                CommonApp.userEntity = list[0]
            }
            var index = 3
            while (index > 0){
                delay(1000)
//                Log.e("","initData:"+index)
                timeStr.set(""+index)
//                Log.e("","timeStr:"+timeStr.get())
                index --
            }
            startData.postValue(BusAction(BusConst.STARTPAGE,null))
        }
    }

}